package org.fast.develop.logicflow.ws.manager;

import lombok.extern.slf4j.Slf4j;
import org.fast.develop.logicflow.runtime.ws.IWebSocketLauncher;
import org.fast.develop.logicflow.runtime.ws.IWsSessionStore;

import javax.websocket.Session;
import java.io.IOException;

/**
 * ws操作相关服务
 */
@Slf4j
public class WebSocketLauncher implements IWebSocketLauncher {

    private WsSessionStore wsSessionStore;

    public WebSocketLauncher(WsSessionStore wsSessionStore) {
        this.wsSessionStore = wsSessionStore;
    }

    /**
     * 发送消息
     * @param key
     * @param text
     * @return
     * @throws IOException
     */
    public void sendMsg(String key, String text) throws IOException {
        wsSessionStore.get(key).getBasicRemote().sendText(text);
    }

    /**
     * 广播消息
     * @param text
     * @return
     * @throws IOException
     */
    public void broadcastMsg(String text) throws IOException {
        for (Session session: wsSessionStore.getSessions()) {
            wsSessionStore.get(session.getId()).getBasicRemote().sendText(text);
        }
    }

    @Override
    public IWsSessionStore getSessionStore() {
        return this.wsSessionStore;
    }

}
